;
(function($, window) {
	var rotateText = [
		'<div class="prize-title prize-success fb">很遗憾没有中奖</div><div class="prize-info prize-error">请再接再厉，下次就能中奖啦</div> ',
		'<div class="prize-title prize-error fb">恭喜您！</div><div class="prize-info prize-error fb f60">下次中奖概率提高</div>',
		'<div class="prize-title prize-success fb">恭喜您！</div><div class="prize-info prize-success fb">您获得了<span class="f60 prize-error">200元京东卡！</span></div>',
		'<div class="prize-title prize-success fb">恭喜您！</div><div class="prize-info prize-success fb">您获得了<span class="f60 prize-error">100元京东卡！</span></div>',
		'<div class="prize-title prize-success fb">很遗憾!</div><div class="prize-info prize-success">奖抽完了</div> ',
		'<div class="prize-title prize-success fb">重复抽奖</div><div class="prize-info prize-error">您已经抽过奖了</div> ',
		'<div class="prize-title prize-success fb">很遗憾!</div><div class="prize-info prize-success">您的抽奖机会已经用完了</div> '
	];
	var rotatePopBtn = [
	    '<a href="/">返回首页继续参与</a>',
	    '<a href="javascript:;" class="closegorotate" onclick="window.location.reload()">继续抽奖</a><a href="/">返回首页继续参与</a>',
	    '<a href="/giftAddress.htm?prizeDes={{prizeDes}}">去填写礼品寄送地址</a>'
	];
	function openPop(_id, gift){
		if($('.js-rotate-text').html() == '') {$('.js-rotate-text').html(rotateText[0])};
		$('.js-totatepop-btn').html('');
		if(gift){
			$('.js-totatepop-btn').html(rotatePopBtn[2].replace('{{prizeDes}}', gift));
		} else {
			if(luckCount*1 <= 0){
				$('.js-totatepop-btn').html(rotatePopBtn[0]);
			} else {
				$('.js-totatepop-btn').html(rotatePopBtn[1]);
			}
		}
		$('#'+_id).show();
	}
	
	var gamePage = {

		init: function() {
			//To avoid repetition and click on the draw button tags
			var bRotate = false;

			//According to the "item" variable function to be executed by the callback
			var rotateFn = function(awards, angles, gift) {
				$('#js_rotate_pointer').stopRotate();
				$('#js_rotate_pointer').rotate({
					angle: 0,
					animateTo: angles + 1800,
					duration: 8000,
					callback: function() {
						openPop('pop-prize', gift);
						bRotate = false;
					}
				});
			};

			//According to the background return values corresponding callback bounced
			var rotateFun = function(item) {

//				var item = rnd(0, 7), //Real environment need to change this function to "AJAX" to obtain the value of "item"
//					_index;
//
//				if (item == 0) {
//					_index = 7;
//				} else {
//					_index = item - 1;
//				}
//				var popText = $(".js_rotate_node").eq(_index).attr('data-pop');
				if(luckCount*1 <= 0){
					$('.js-totatepop-btn').html(rotatePopBtn[0]);
				} else {
					$('.js-totatepop-btn').html(rotatePopBtn[1]);
				}
				switch (item) {
					case 2:
						$('.js-rotate-text').html(rotateText[2]);
						rotateFn(338.5, 22.5, 200);
						break;
					case 3:
						$('.js-rotate-text').html(rotateText[0]);
						rotateFn(23.5, 67.5);
						break;
					case 4:
						$('.js-rotate-text').html(rotateText[1]);
						rotateFn(68.5, 112.5);
						break;
					case 5:
						$('.js-rotate-text').html(rotateText[3]);
						rotateFn(113.5, 157.5, 100);
						break;
					case 6:
						$('.js-rotate-text').html(rotateText[2]);
						rotateFn(158.5, 202.5, 200);
						break;
					case 7:
						$('.js-rotate-text').html(rotateText[0]);
						rotateFn(203.5, 247.5);
						break;
					case 0:
						$('.js-rotate-text').html(rotateText[1]);
						rotateFn(248.5, 292.5);
						break;
					case 1:
						$('.js-rotate-text').html(rotateText[3]);
						rotateFn(293.5, 337.5, 100);
						break;
				}
			}

			//Test with random integer return 1-6
			var rnd = function(n, m) {
				return Math.floor(Math.random() * (m - n + 1) + n)
			};

			//Click on the draw button to perform the function
			$('#js_rotate_btn').click(function() {
				// var item = rnd(1, 6);
				$('.js-totatepop-btn').html('');
				$('.js-rotate-text').html('');

				if (bRotate) return;
				bRotate = true;
				if(luckCount*1 <= 0){
					$('.js-rotate-text').html(rotateText[6]);
					$('.js-totatepop-btn').html(rotatePopBtn[0]);
					openPop('pop-prize');
					bRotate = false;
					return;
				}
				$.ajax({
					type: "GET",
					url: '/salePrize.htm?33',
					data: {
					},
					dataType: "JSON",
					success: function(data) {
//						var data = {code: 3};
						//code 0：中奖了，1:没中，下次继续,2:很遗憾,奖抽完了，3：重复抽奖,4:无抽奖机会
						var code = data.code;
						var rotateLevel = data.data;
						var rotateIndex = false;
//						code = 0;
//						rotateLevel = '200';
						switch (code) {
							case 1:
								rotateIndex = rnd(3, 4);
								break;
							case 2:
								$('.js-rotate-text').html(rotateText[4]);
								$('.js-totatepop-btn').html(rotatePopBtn[1]);
								openPop('pop-prize');
								bRotate = false;
								return;
							case 3:
								$('.js-rotate-text').html(rotateText[5]);
								$('.js-totatepop-btn').html(rotatePopBtn[1]);
								openPop('pop-prize');
								bRotate = false;
								return;
							case 4:
								$('.js-rotate-text').html(rotateText[6]);
								$('.js-totatepop-btn').html(rotatePopBtn[1]);
								openPop('pop-prize');
								bRotate = false;
								return;
							case 0:
								if(rotateLevel == 100){
									rotateIndex = 5;
								} else if(rotateLevel == 200){
									rotateIndex = 2;
								}
								break;
						}

						if(rotateIndex === false){
							ECar.dialogPrompt({
								text: '抽奖出错了！',
								clickClose: true
							});
							bRotate = false;
						} else {
							luckCount = luckCount*1 - 1;
							rotateFun(rotateIndex);
						}
					},
					error: function() {
						ECar.dialogPrompt({
							text: '抽奖出错了！',
							clickClose: true
						});
						bRotate = false;
					}
				});

			});

		}

	};


	gamePage.run = {
		loadLotteryDate: function() {
			gamePage.dev.getLotteryData(function(data) {
				if (data.status == 1) {
					$.each(data.info, function(index, item) {
						var popKey = "pop-the" + item.type + "ndprize";
						if (item.type == 1) popKey = "pop-firstprize";
						if (item.type == 0) popKey = "pop-losingprize";
						if (item.type == 2) popKey = "pop-the2ndprize";
						if (item.type == 3) popKey = "pop-the3ndprize";
						if (item.type == 4) popKey = "pop-the4ndprize";
						if (item.type == 5) popKey = "pop-the5ndprize";
						$(".js_rotate_node").eq(index).attr("data-code", item.code).attr("data-pop", popKey).html(item.text);
					});
					gamePage.init();
				} else {
					alert(data.message);
					//openPop();
				}
			});
		}
	};
	gamePage.dev = {
		getLotteryData: function(callback) {
			//code：lottery id
			//text：lottery des
			//type: Dialog box is displayed.
			var data = [{
				code: 1,
				type: 1,
				text: "一等奖"
			}, {
				code: "a",
				type: 2,
				text: "二等奖"
			}, {
				code: 3,
				type: 3,
				text: "三等奖"
			}, {
				code: 4,
				type: 4,
				text: "四等奖"
			}, {
				code: 5,
				type: 5,
				text: "五等奖"
			}, {
				code: 0,
				type: 0,
				text: "谢谢"
			}];
			try {
				getLotteryData(callback);
			} catch (e) {
				callback({
					status: 1,
					message: "",
					info: data
				});
			}
		},
		getLotteryResult: function(callback) {
			try {
				getLotteryResult(callback);
			} catch (e) {
				callback({
					status: 1,
					message: "",
					lotteryId: "a"
				})
			}
		}
	};
//	gamePage.run.loadLotteryDate();

	gamePage.init();

})($, window);